 <#-- 
 
 crea una clase para c# para la tabla  ${table}...
 Author : Luis Molina...
 Project: ${project}
 Table :  ${table}
 -->
<#-- variables used internally by the generator... -->
<#assign nameFile="dao">
<#assign extensionFile="java">
<#assign languageGenerated="java">
<#assign description="javadao">
<#assign targetDirectory="src\\java\\classes">
<#assign appliesToAllTables="true">
<#-- END variables used internally by the generator... -->


package ${project};


//import java.sql.Connection;
//import java.sql.PreparedStatement;
//import java.util.Iterator;

import java.sql.SQLException;
import java.util.List;
import java.sql.ResultSet;
import java.util.Date;

import classesBase.dbClass;
import classesBase.sf;


<#-- 
 
 we need the keyfield...
 -->
<#list table.getFields() as field>
         <#if (field.getisKey())>
            <#assign keyField=field>
            </#if>
     </#list>


/**
 *
 * @author Luis Molina Martinez
 */
 

public class ${table} {

<#list table.getFields() as field>
	 
		 <#-- segun el tipo de campo...   -->
		 <#switch field.getType()>
			  <#case "_int">
			     private int _${field};
			     <#break>
			  <#case "_string">
			     private String _${field};
			     <#break>
			  <#case "_date">
			     private Date _${field};
			     <#break>
			  <<#case "_boolean">
			     private boolean _${field};
			     <#break>
			  <#default>
			     private String _${field};
		</#switch>  
	 
	</#list>
    
	
	
	<#list table.getFields() as field>
	 
		 <#-- segun el tipo de campo...   -->
		 <#switch field.getType()>
			  <#case "_int">
			      public int get${field?capitalize}() {
				        return _${field};
				    }

				    public void set${field?capitalize}(int ${field}) {
				        this._${field} = ${field};
				    }
			     <#break>
			  <#case "_string">
			      public String get${field?capitalize}() {
				        return _${field};
				    }

				    public void set${field?capitalize}(String ${field}) {
				        this._${field} = ${field};
				    }
			     <#break>
			  <#case "_date">
			      public Date get${field?capitalize}() {
				        return _${field};
				    }

				    public void set${field?capitalize}(Date ${field}) {
				        this._${field} = ${field};
				    }
			     <#break>
			  <<#case "_boolean">
			      public boolean get${field?capitalize}() {
				        return _${field};
				    }

				    public void set${field?capitalize}(boolean ${field}) {
				        this._${field} = ${field};
				    }
			     <#break>
			  <#default>
			      
			      public String get${field?capitalize}() {
				        return _${field};
				    }

				    public void set${field?capitalize}(String ${field}) {
				        this._${field} = ${field};
				    }
		</#switch>  
	 
	</#list>	
  
  
    public ${table}() {
    }
	
	
	
	public void delete${table?capitalize} (${table} obj${table} ){
		String sql = "delete * FROM ${table} where (`${keyField}` = %s)";
        String finalSql = String.format(sql, sf.cadena(obj${table}.get${keyField?capitalize}()));
		dbClass.executeQuery(finalSql); 

	}
	
	public void delete${table?capitalize} (int ${keyField}){
		String sql = "delete * FROM ${table} where (`${keyField}` = %s)";
        String finalSql = String.format(sql, sf.cadena(${keyField}));
		dbClass.executeQuery(finalSql); 

	}
	
	
	public void insert${table?capitalize} (${table} obj${table} ){
		String sql="";
		sql=sql.concat( " insert into ${table} (${table.getListFieldsWithQuotes()})"); 
        sql=sql.concat( " values (");

			<#assign count=0>	
			<#list table.getFields() as field> 
				<#if (!field.getisKey())>
					 <#-- segun el tipo de campo...   -->
					 <#switch field.getType()>
							  <#case "_int">
			 					 sql=sql.concat( sf.mySql(obj${table}.get${field?capitalize}()) + " ,") ;
								 <#break>
							  <#case "_string">
								 sql=sql.concat( sf.mySql(obj${table}.get${field?capitalize}()) + " ,") ;
								 <#break>
							  <#case "_date">
								 sql=sql.concat( sf.mySql(obj${table}.get${field?capitalize}()) + " , ")  ;
								 <#break>
							  <<#case "_boolean">
								 sql=sql.concat( sf.mySql(obj${table}.get${field?capitalize}()))  + " ,";
								 <#break>
							  <#default>
								 sql=sql.concat( sf.mySql(obj${table}.get${field?capitalize}())) + " ," ;
					</#switch>  
				</#if>
			 <#assign count = count+1>
			</#list>
			// quit the last ,
		sql = sql.substring(0,sql.length()-1);
		sql=sql.concat(" )") ;
		dbClass.excecuteQuery(sql); 

	}
	
	public static void update${table?capitalize} (${table} obj${table} ){
		String sql="";
		sql=sql.concat( " update ${table} set "); 
        
			<#assign count=0>	
			<#list table.getFields() as field> 
				<#if (!field.getisKey())>
					 <#-- segun el tipo de campo...   -->
					 <#switch field.getType()>
							  <#case "_int">
			 					 sql=sql.concat( "`${field?capitalize}`=" + sf.mySql(obj${table}.get${field?capitalize}()) + " ,") ;
								 <#break>
							  <#case "_string">
								 sql=sql.concat( "`${field?capitalize}`=" +   sf.mySql(obj${table}.get${field?capitalize}()) + " ,") ;
								 <#break>
							  <#case "_date">
								 sql=sql.concat( "`${field?capitalize}`=" +   sf.mySql(obj${table}.get${field?capitalize}()) + " , ")  ;
								 <#break>
							  <<#case "_boolean">
								 sql=sql.concat( "`${field?capitalize}`=" + sf.mySql(obj${table}.get${field?capitalize}()))  + " ,";
								 <#break>
							  <#default>
								 sql=sql.concat(  "`${field?capitalize}`="+  sf.mySql(obj${table}.get${field?capitalize}())) + " ," ;
					</#switch>  
				</#if>
			 <#assign count = count+1>
			</#list>
			// quit the last ,
		sql = sql.substring(0,sql.length()-1);
		sql=sql.concat(" where `${keyField}`=" + sf.mySql(obj${table}.get${keyField}())) ;
		dbClass.excecuteQuery(sql); 

	}
	
    
	
	public static List<${table}> getList () {
		
		final List<${table}> ${table}List = null;
        ${table} p1 = null;
        
		ResultSet result = null;
        String sql = "SELECT * FROM ${table} ";
         
        try {

            result = dbClass.getResultsFromQuery(sql);

            while (result.next()){
                p1 = new ${table}();
				<#assign count=0>
				<#list table.getFields() as field>
				 
				     <#-- segun el tipo de campo...   -->
				     <#switch field.getType()>
				              <#case "_int">
				                 p1.set${field?capitalize}(result.getInt("${field}"));
				                 <#break>
				              <#case "_string">
				                 p1.set${field?capitalize}(result.getString("${field}"));
				                 <#break>
				              <#case "_date">
				                 p1.set${field?capitalize}(result.getDate("${field}"));
				                 <#break>
				              <#case "_boolean">
				                 p1.set${field?capitalize}(result.getBoolean("${field}"));
				                 <#break>
				              <#case "_float">
				                 p1.set${field?capitalize}(result.getFloat("${field}"));
				                 <#break>
				              <#default>
				                 p1.set${field?capitalize}(result.getString("${field}"));
				    </#switch>  
				    <#assign count = count+1>
				</#list>
				${table}List.add(p1);
                

           }
        } catch (Exception e) {
            System.out.println("Error en la consulta Sql: " + e.getMessage());
        } finally {
        }

        return ${table}List;
    }

	 public static  ${table} get${table?capitalize} (int ${keyField}) {

        ${table} p1 = null;
        ResultSet result = null;
        String sql = "SELECT * FROM ${table} where (${keyField} = %s)";
        String finalSql = String.format(sql, sf.cadena(${keyField}));
        try {

            result = dbClass.getResultsFromQuery(finalSql);

            if (result.next()) {
                p1 = new ${table}();
				<#assign count=0>
				<#list table.getFields() as field>
				 
				     <#-- segun el tipo de campo...   -->
				     <#switch field.getType()>
				              <#case "_int">
				                 p1.set${field?capitalize}(result.getInt("${field}"));
				                 <#break>
				              <#case "_string">
				                 p1.set${field?capitalize}(result.getString("${field}"));
				                 <#break>
				              <#case "_date">
				                 p1.set${field?capitalize}(result.getDate("${field}"));
				                 <#break>
				              <#case "_boolean">
				                 p1.set${field?capitalize}(result.getBoolean("${field}"));
				                 <#break>
				              <#case "_float">
				                 p1.set${field?capitalize}(result.getFloat("${field}"));
				                 <#break>
				              <#default>
				                 p1.set${field?capitalize}(result.getString("${field}"));
				    </#switch>  
				    <#assign count = count+1>
				</#list>
                

            } else {
				System.out.println("No hay car con el id " + sf.cadena(id${table})  + "  en la bd");
				return null;
            }
        } catch (SQLException e) {
            System.out.println("Error en la consulta Sql: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        }

        return p1;
    }
	
	
	
	
		  
 
}